<template>
  <div class="app-container">
    <!--表单-->
    <el-form :inline="true" class="demo-form-inline" align="center">
      <el-form-item>
        <el-input v-model="searchObj.hosname" placeholder="医院名称"/>
      </el-form-item>

      <el-form-item>
        <el-input v-model="searchObj.hoscode" placeholder="医院编号"/>
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="fetchData()"
      >查询
      </el-button
      >
      <el-button type="default" @click="resetData()">清空</el-button>
      <!-- 1.2工具条 -->
      <el-button type="danger" @click="removeRows()">批量删除</el-button>
    </el-form>

    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      stripe
      style="width: 100%"
      element-loading-text="数据加载中"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="医院设置列表" align="center">
        <!-- 每行记录前添加一个复选框 -->
        <el-table-column type="selection" width="55"/>
        <el-table-column label="序号" width="70" align="center">
          <template slot-scope="scope">
            {{ (page - 1) * limit + scope.$index + 1 }}
          </template>
        </el-table-column>

        <el-table-column prop="hosname" label="医院名称" width="180"/>

        <el-table-column prop="hoscode" label="医院编号" width="160"/>

        <el-table-column prop="apiUrl" label="地址" width="200"/>

        <el-table-column prop="contactsName" label="联系人"/>

        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            {{ scope.row.status === 1 ? "可用" : "不可用" }}
          </template>
        </el-table-column>

        <el-table-column label="操作" width="280" align="center">
          <template slot-scope="scope">
            <el-button v-if="scope.row.status==1" type="info" size="mini"
                       icon="el-icon-lock" @click="lockHospSet(scope.row.id,0)">锁定
            </el-button>
            <el-button v-if="scope.row.status==0" type="warning" size="mini"
                       icon="el-icon-unlock" @click="lockHospSet(scope.row.id,1)">解锁
            </el-button>
            &nbsp;
            <!-- 修改页面思路:
                  1.查询数据
                  2.数据回显
                  3.跳转页面(隐藏路由、路由跳转)
             -->
            <router-link :to="'/hospset/update/' + scope.row.id">
              <el-button type="primary" size="mini" icon="el-icon-edit"
              >修改
              </el-button>
            </router-link>

            &nbsp;
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="removeDataById(scope.row.id)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />
  </div>
</template>

<script>
import hospSet from "@/api/hospital/hospset";

export default {
  data() {
    return {
      listLoading: true, // 是否显示loading信息
      total: 0, // 总记录数
      list: null, // 每页数据list
      page: 1, // 当前页
      limit: 10, // 每页记录数
      searchObj: {}, // 查询条件
      multipleSelection: [], // 复选框选中的记录
    }
  },
  created() {
    this.fetchData();
  },
  methods: {
    /**
     * 锁定和取消锁定思路:
     *    1. 点击按钮传递状态给后端操作
     *    2. 操作完后,重新获取页面
     */
    lockHospSet(id, status) {
      hospSet.lockHospSet(id, status)
        .then(response => {
          this.fetchData()
        })
    },

    /**
     * 批量删除思路：
     *    1. 每行添加复选框
     *    2. el会自动获得复选框所选行的记录
     *    3. 从对象集合中获取所有的id值集合,封装成id集合
     *    4. 将id集合传递给后端处理
     */
    removeRows(id) {
      this.$confirm("此操作将批量永久删除医院信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        var idList = [];
        //遍历数组得到每个id值，设置到idList里面
        for (var i = 0; i < this.multipleSelection.length; i++) {
          // 从this.multipleSelection数组获取所有id值
          var obj = this.multipleSelection[i];
          var id = obj.id;
          idList.push(id);
        }
        hospSet.batchRemoveHospitalSet(idList).then((response) => {
          this.fetchData();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        });
      });
    },

    /**
     * 当表格复选框选项发生变化时触发handleSelectionChange方法
     */
    handleSelectionChange(selectionCount) {
      // selectionCount会获取复选框所在行的内容
      this.multipleSelection = selectionCount;
    },

    /**
     * 根据id删除医院信息思路:
     * 1. 弹出删除确认框
     * 2. 删除执行
     * 3. 提示删除成功
     */
    removeDataById(id) {
      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        return hospSet.removeById(id);
      }).then(() => {
        this.fetchData();
        this.$message({
          type: "success",
          message: "删除成功!",
        });
      });
    },

    /**
     * 清空表单思路:
     * 1. 清空表单
     * 2. 查询所有数据
     */
    resetData() {
      this.searchObj = {};
      this.fetchData();
    },

    /**
     * 医院设置列表,条件查询带分页
     * @param page
     */
    fetchData(page = 1) {
      this.page = page
      hospSet.getPageList(this.page, this.limit, this.searchObj)
        .then(response => {
          //每页数据集合
          this.list = response.data.rows
          //总记录数
          this.total = response.data.total
          //加载图表不显示
          this.listLoading = false
        })
    },
  },

}
</script>
